﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DWEIP.Web.Core;
using DWEIP.Common;
using DWEIP.Models.OA;
using DWEIP.BLL.OA;
using DWEIP.Locale;


namespace DWEIP.Web.Areas.OA.Controllers
{
    public class Ncalm001Controller : BaseController
    {
        ValidationErrors errors = new ValidationErrors();
        public OA_CalBLL m_BLL = new OA_CalBLL();

        [SupportFilter]
        public ActionResult Index()
        {
            return View();
        }

        [HttpGet]
        public JsonResult GetList(DateTime start, DateTime end)
        {
            List<SysCalendarPlan> list = m_BLL.GetList(ref setNoPagerDescByCreateTime, start, end, GetUserId());
            var json = (from r in list
                        select new CalendarPlanJsonModel()
                        {
                            id = r.Id,
                            title = r.Title,
                            content = r.PlanContent,
                            start = r.BeginDate,
                            end = r.EndDate,
                            url = r.Url,
                            color = r.Color,
                            textColor = r.TextColor
                        }).ToArray();
            return Json(json, JsonRequestBehavior.AllowGet);
        }

        #region 创建
        //[SupportFilter]
        public ActionResult Create(string start, string end)
        {
            DateTime beginDate = new DateTime();
            DateTime endDate = new DateTime();
            if (start == null)
            {
                beginDate = Convert.ToDateTime(ResultHelper.NowTime.ToShortDateString());
                endDate = Convert.ToDateTime(ResultHelper.NowTime.AddDays(1).ToShortDateString());
            }
            else
            {
                beginDate = Convert.ToDateTime(start);
                endDate = Convert.ToDateTime(end);
            }


            SysCalendarPlan model = new SysCalendarPlan()
            {
                BeginDate = beginDate,
                EndDate = endDate
            };
            return View(model);
        }

        [HttpPost]
        //[SupportFilter]
        public JsonResult Create(SysCalendarPlan model)
        {
            model.Id = ResultHelper.NewId;
            model.CreateTime = ResultHelper.NowTime;
            model.SysUserId = GetUserId();
            model.Editable = model.Editable == null ? "false" : model.Editable;
            if (model != null && ModelState.IsValid)
            {

                if (m_BLL.Create(ref errors, model))
                {
                    LogHandler.WriteServiceLog(GetUserId(), "Id" + model.Id + ",Title" + model.Title, "成功", "创建", "SysCalendarPlan");
                    return Json(JsonHandler.CreateMessage(1, Resource.InsertSucceed));
                }
                else
                {
                    string ErrorCol = errors.Error;
                    LogHandler.WriteServiceLog(GetUserId(), "Id" + model.Id + ",Title" + model.Title + "," + ErrorCol, "失败", "创建", "SysCalendarPlan");
                    return Json(JsonHandler.CreateMessage(0, Resource.InsertFail + ErrorCol));
                }
            }
            else
            {
                return Json(JsonHandler.CreateMessage(0, Resource.InsertFail));
            }
        }
        #endregion
    }
}